﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeMakeSP.ImageResizer.Configuration;
using WeMakeSP.ImageResizer.UI.Models;
using WeMakeSP.ImageResizer.UI.Views;
using WeMakeSP.Platform.SharePoint.Web.UI.Presenters;

namespace WeMakeSP.ImageResizer.UI.Presenters
{
    /// <summary>
    /// The implementation of the image resizer settings presenter.
    /// </summary>
    public class ImageResizerSettingsPresenter : PresenterPageBase<IImageResizerSettingsView>, IImageResizerSettingsPresenter
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageResizerSettingsPresenter"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="model">The model.</param>
        public ImageResizerSettingsPresenter(IImageResizerSettingsView view, IImageResizerSettingsModel model)
            : base(view)
        {
            // Guard.

            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            // Set the model.

            this.Model = model;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        public IImageResizerSettingsModel Model
        {
            get;
            private set;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Processes the request.
        /// </summary>
        public void ProcessRequest()
        {
            // Get the configuration from the view.

            ImageResizerPictureLibraryConfiguration configuration =
                new ImageResizerPictureLibraryConfiguration()
                {
                    Active = this.View.ConfigurationEnabled,
                    MaxHeight = this.View.MaxHeight,
                    MaxWidth = this.View.MaxWidth,
                    Mode = this.View.Mode,
                    Schedule = this.View.Schedule,
                    HoursDelay = this.View.HoursDelay,
                    MinutesDelay = this.View.MinutesDelay,
                    HourSchedule = this.View.HourSchedule,
                    MinuteSchedule = this.View.MinuteSchedule,
                    SecondsDelay = 0
                };

            // Update the configuration.

            this.Model.SetPictureLibraryConfiguration(this.View.ListId, configuration);

            // Go back the previous page.

            this.View.RedirectToSource();
        }

        #endregion

        #region Protected Override Methods

        /// <summary>
        /// Called when [view initialized].
        /// </summary>
        public override void OnViewInitialized()
        {
            base.OnViewInitialized();
            
            // Load the configurations.

            ImageResizerPictureLibraryConfiguration configuration = null;

            try
            {
                configuration =
                    this.Model.GetPictureLibraryConfiguration(this.View.ListId);
            }
            catch
            {
                // Use default configuration.

                configuration = 
                    new ImageResizerPictureLibraryConfiguration();
            }

            // Set properties.

            this.View.ConfigurationEnabled = configuration.Active;
            this.View.MaxHeight = configuration.MaxHeight;
            this.View.MaxWidth = configuration.MaxWidth;
            this.View.Mode = configuration.Mode;
            this.View.Schedule = configuration.Schedule;
            this.View.HoursDelay = configuration.HoursDelay;
            this.View.MinutesDelay = configuration.MinutesDelay;
            this.View.HourSchedule = configuration.HourSchedule;
            this.View.MinuteSchedule = configuration.MinuteSchedule;
        }

        /// <summary>
        /// Called when [view loaded].
        /// </summary>
        public override void OnViewLoaded()
        {
            base.OnViewLoaded();
        }

        #endregion
    }
}
